package gui;

import java.io.IOException;

import gui.threads.AddSongThread;

import org.apache.commons.compress.archivers.ArchiveException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import parser.UpdateByFolder;

import LogicSide.Recommendation;
import LogicSide.Session;


public class Edit {
	
	private static int labelStyle = SWT.CENTER;
	private static int textBoxStyle = SWT.SINGLE | SWT.BORDER;
	
	
	public static Composite createEdit(final Shell shell){
		//update 
		Main.inSearch = false;
		
		
		Composite editComposite = new Composite(shell, SWT.NONE);
		editComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		editComposite.setLayout(new GridLayout(1, false));
		
		
		/**
		 * add song via information: name, album, artist, genre(text) and main genre(from combo)
		 * 
		 * delete song via id
		 * delete album via id
		 * delete artist via id
		 * 
		 * update file/folder via text specifying path
		 */
		
		Composite addSongComposite = new Composite(editComposite, SWT.BORDER);
		addSongComposite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_MAGENTA));
		addSongComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		addSongComposite.setLayout(new GridLayout(4, true));
		//add title
		Label titleLabel = new Label(addSongComposite, labelStyle | SWT.CENTER);
		titleLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		titleLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1));
		titleLabel.setText("Add song to database manually");
		//Add song composite input fields
		Label songLabel = new Label(addSongComposite, labelStyle);
		songLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		songLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		songLabel.setText("Song name: ");
		
		final Text songText = new Text(addSongComposite, textBoxStyle);
		songText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		Label artistLabel = new Label(addSongComposite, labelStyle);
		artistLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		artistLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		artistLabel.setText("Artist name: ");
		
		final Text artistText = new Text(addSongComposite, textBoxStyle);
		artistText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		Label albumLabel = new Label(addSongComposite, labelStyle);
		albumLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		albumLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		albumLabel.setText("Album name: ");
		
		final Text albumText = new Text(addSongComposite, textBoxStyle);
		albumText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		Label genreLabel = new Label(addSongComposite, labelStyle);
		genreLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		genreLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		genreLabel.setText("genre name: ");
		
		final Text genreText = new Text(addSongComposite, textBoxStyle);
		genreText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		Label mainGenreLabel = new Label(addSongComposite, labelStyle);
		mainGenreLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		mainGenreLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		mainGenreLabel.setText("Choose main genre:");
		
		final Combo genreCombo = new Combo(addSongComposite, SWT.READ_ONLY);
		genreCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		Recommendations.fillGenreCombo(genreCombo);
		//
		//Add song button
		Button addSongButton = new Button(addSongComposite, SWT.PUSH | SWT.FLAT);
		addSongButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
		addSongButton.setText("Add");
		//
		addSongButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Utils.loadingCursor(Main.display, shell);
				if(checkFields(songText.getText(), artistText.getText(), albumText.getText(), 
						genreText.getText(), genreCombo)){
					new AddSongThread(songText.getText(), artistText.getText(), albumText.getText(),
							genreCombo.getItem(genreCombo.getSelectionIndex()),
							genreText.getText()).start();
				}
				else{
					Utils.popUpBox("Error", "All fields must be filled!", shell);
				}
			}
		});
		
		
		/**
		 * UPDATE via file
		 */
		
		Composite updateFileComposite = new Composite(editComposite, SWT.BORDER);
		updateFileComposite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_MAGENTA));
		updateFileComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		updateFileComposite.setLayout(new GridLayout(3, true));
		
		//add title
		Label title2Label = new Label(updateFileComposite, labelStyle | SWT.CENTER);
		title2Label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		title2Label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		title2Label.setText("Add to database via file (bz2/ freeDBfile / folder)");
		//
		
		Label updateFileLabel = new Label(updateFileComposite, labelStyle);
		updateFileLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		updateFileLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		updateFileLabel.setText("File path: ");
		
		final Text updateFileText = new Text(updateFileComposite, textBoxStyle);
		updateFileText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		Button updateFileButton = new Button(updateFileComposite, SWT.PUSH | SWT.FLAT);
		updateFileButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		updateFileButton.setText("Update");
		
		updateFileButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Utils.loadingCursor(Main.display, shell);
				final String path = updateFileText.getText();
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						try {
							UpdateByFolder.fileToDatabase(path, Main.session);
							Utils.regularCursor(Main.display, shell);
						} catch (IOException e) {
							Main.display.asyncExec(new Runnable() {
								
								@Override
								public void run() {
									Utils.regularCursor(Main.display, shell);
									Utils.popUpBox("Failure", "Couldn't open file", shell);
								}
							});
						} catch (ArchiveException e) {
							Main.display.asyncExec(new Runnable() {
								@Override
								public void run() {
									Utils.regularCursor(Main.display, shell);
									Utils.popUpBox("Failure", "ArchiveException", shell);
								}
							});
						}
						
					}
				}).start();
			}
		});
		
		
		
		
		
		
		
		return editComposite;
	}


	protected static boolean checkFields(String text, String text1, String text2,
			String text3, Combo genreCombo) {
		if(text.isEmpty() || text1.isEmpty() || text2.isEmpty() || text3.isEmpty())
			return false;
		if(genreCombo.getSelectionIndex() == -1)
			return false;
		return true;
	}
	
	

	

}
